package com.lly.songhigh.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.entity.AlbumDetials;

import java.util.List;

/**
 * Created by 李海龙 on 15/10/30.
 */
public class AlbumDetialsRecycleViewAdapter extends RecyclerView.Adapter<AlbumDetialsRecycleViewAdapter.ViewHolder> {

    private Context context;
    private List<AlbumDetials.SongListEntity> songListEntities;
    private OnMenuClickListener onMenuClickListener;
    private OnItemListener onItemListener;

    public AlbumDetialsRecycleViewAdapter(Context context, List<AlbumDetials.SongListEntity> songListEntities) {
        this.context = context;
        this.songListEntities = songListEntities;
    }

    public void setOnMenuClickListener(OnMenuClickListener onMenuClickListener) {
        this.onMenuClickListener = onMenuClickListener;
    }

    public void setOnItemListener(OnItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    public interface OnItemListener {
        void onItemClick(AlbumDetials.SongListEntity songListEntity, int position);
    }

    public interface OnMenuClickListener {
        void onMenuClick(AlbumDetials.SongListEntity songListEntity);
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.album_recycleview_item, null);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        AlbumDetials.SongListEntity entity = songListEntities.get(position);

        holder.albumSong_collectionCount.setText(entity.getFavorites() + "");
        holder.albumSong_singerName.setText(entity.getSingerName());
        holder.albumSong_name.setText(entity.getName());
        holder.albumSong_position.setText(position + 1 + "");
        if (entity.getAuditionList().size() != 3) {
            holder.albumSong_HQ.setVisibility(View.GONE);
        }
        holder.position = position;
    }

    @Override
    public int getItemCount() {
        return songListEntities != null && songListEntities.size() > 0? songListEntities.size() : 0;
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView albumSong_position, albumSong_name, albumSong_singerName, albumSong_collectionCount;
        private ImageView albumSong_HQ, album_detials_rightArr;
        private int position;
        private RelativeLayout rl_album_detials_item;

        public ViewHolder(View itemView){
            super(itemView);
            albumSong_position = (TextView) itemView.findViewById(R.id.albumSong_position);
            albumSong_collectionCount = (TextView) itemView.findViewById(R.id.albumSong_collectionCount);
            albumSong_name = (TextView) itemView.findViewById(R.id.albumSong_name);
            albumSong_singerName = (TextView) itemView.findViewById(R.id.albumSong_singerName);
            albumSong_HQ = (ImageView) itemView.findViewById(R.id.albumSong_HQ);
            rl_album_detials_item = (RelativeLayout) itemView.findViewById(R.id.rl_album_detials_item);
            album_detials_rightArr = (ImageView) itemView.findViewById(R.id.album_detials_rightArr);

            album_detials_rightArr.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onMenuClickListener != null) {
                        AlbumDetials.SongListEntity songListEntity = songListEntities.get(position);
                        onMenuClickListener.onMenuClick(songListEntity);
                    }
                }
            });

            rl_album_detials_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemListener != null) {
                        AlbumDetials.SongListEntity songListEntity = songListEntities.get(position);
                        onItemListener.onItemClick(songListEntity, position);
                    }
                }
            });


        }

    }

//    private Context context;
//
//    private List<AlbumDetials.SongListEntity> songListEntities;
//
//    public AlbumDetialsRecycleViewAdapter(Context context, List<AlbumDetials.SongListEntity> songListEntities) {
//        this.context = context;
//        this.songListEntities = songListEntities;
//    }
//
//
//    @Override
//    public int getCount() {
//        return songListEntities != null && songListEntities.size() > 0 ? songListEntities.size() : 0;
//    }
//
//    @Override
//    public Object getItem(int position) {
//        return songListEntities.get(position);
//    }
//
//    @Override
//    public long getItemId(int position) {
//        return position;
//    }
//
//    @Override
//    public View getView(int position, View convertView, ViewGroup parent) {
//        ViewHolder holder;
//        if (convertView == null) {
//            convertView = LayoutInflater.from(context).inflate(R.layout.album_recycleview_item, null);
//            holder = new ViewHolder();
//            holder.albumSong_position = (TextView) convertView.findViewById(R.id.albumSong_position);
//            holder.albumSong_collectionCount = (TextView) convertView.findViewById(R.id.albumSong_collectionCount);
//            holder.albumSong_name = (TextView) convertView.findViewById(R.id.albumSong_name);
//            holder.albumSong_singerName = (TextView) convertView.findViewById(R.id.albumSong_singerName);
//            holder.albumSong_HQ = (ImageView) convertView.findViewById(R.id.albumSong_HQ);
//            convertView.setTag(holder);
//        }else {
//            holder = (ViewHolder) convertView.getTag();
//        }
//
//        AlbumDetials.SongListEntity entity = songListEntities.get(position);
//        holder.albumSong_collectionCount.setText(entity.getFavorites() + "");
//        holder.albumSong_singerName.setText(entity.getSingerName());
//        holder.albumSong_name.setText(entity.getName());
//        holder.albumSong_position.setText(position + 1 + "");
//        if (entity.getAuditionList().size() != 3) {
//            holder.albumSong_HQ.setVisibility(View.GONE);
//        }
//        return convertView;
//    }
//
//    private class ViewHolder {
//
//        private TextView albumSong_position, albumSong_name, albumSong_singerName, albumSong_collectionCount;
//        private ImageView albumSong_HQ;
//    }

}
